// 公用函数
/* eslint-disable */
function getParam (name, url) {
  if (!url) url = location.href
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS)
  var results = regex.exec(url)
  return results == null ? null : results[1]
}
export function getUrlCode (value) {
  let currentUrl = window.location.href
  let code = getParam(value, currentUrl)
  if (value == 'token' && code) {
    localStorage.setItem('token', code)
  }
  return code
}

var timerSetDebucens = null;
export const testDebucens = (fn, wait = 500) => {
  return function (...args) {
    clearTimeout(timerSetDebucens);
    timerSetDebucens = setTimeout(() => {
      fn.call(this, ...args);
    }, wait);
  };
};
if (timerSetDebucens) clearTimeout(timerSetDebucens);


export function uploading (bool, msg) {
  var html = '<div style="width:200px;height:55px;position:fixed;top:30%;left:45%;z-index:9999999;' +
    'background-color:rgba(0,0,0,0.7);text-align:center;line-height:55px;border-radius:3px;color:#FFFFFF;" id="imgUpload">' + msg + '</div>'
  if (bool) {
    $("body").append(html);
  }
  else {
    if ($("#imgUpload")) {
      $("#imgUpload").remove()
    }
  }
}
export function download (sUrl) {

  //iOS devices do not support downloading. We have to inform user about this.
  if (/(iP)/g.test(navigator.userAgent)) {
    alert('Your device does not support files downloading. Please try again in desktop browser.');
    return false;
  }

  //If in Chrome or Safari - download via virtual link click

  //Creating new link node.
  var link = document.createElement('a');
  link.href = sUrl;

  if (link.download !== undefined) {
    //Set HTML5 download attribute. This will prevent file from opening if supported.
    var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
    link.download = fileName;
  }

  //Dispatching click event.
  if (document.createEvent) {
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, true);
    link.dispatchEvent(e);
    return true;
  }


  // Force file download (whether supported by server).
  if (sUrl.indexOf('?') === -1) {
    sUrl += '?download';
  }

  //window.open(sUrl, '_self');
  window.open(sUrl, '_blank');
  return true;

}

export function jumpUrl (url) {
  var link = document.createElement('a')
  var body = document.querySelector('body')
  link.href = url
  link.target = '_blank'
  // fix Firefox
  link.style.display = 'none'
  body.appendChild(link)
  link.click()
  body.removeChild(link)
  window.URL.revokeObjectURL(link.href)
}

export function getResDomain () {
  let domain = localStorage.getItem('resourceDomain')
  if (domain && domain.lastIndexOf('/') === domain.length - 1) {
    domain = domain.slice(0, -1)
  }
  return domain
}



